package com.example.kam.mymediapalyer;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.kam.mymediapalyer.service.PlayFileService;
import com.example.kam.mymediapalyer.util.Player;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import jcifs.smb.SmbFile;


public class MainActivity extends Activity {
    private ListView mListView;
    private ListViewAdapter mAdapter;
    private Button ok;
    private List<Integer> haveSee = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, PlayFileService.class);
        startService(intent);
        mListView = (ListView) findViewById(R.id.listView);
        mAdapter = new ListViewAdapter(this);
        mListView.setAdapter(mAdapter);

        new CheckRootThread(new CheckRootThread.CallBack() {
            @Override
            public void callFiles(SmbFile[] files) {
                mAdapter.setCurrentFiles(Arrays.asList(files));
            }
        }, "smb://192.168.1.100/").start();

        findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.saveData();
                if (DataHolder.files.size() < 1) {
                    Toast.makeText(MainActivity.this, "木有视屏", Toast.LENGTH_LONG).show();
                }
            }
        });

        findViewById(R.id.play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (DataHolder.files.size() < 1) {
                    Toast.makeText(MainActivity.this, "木有设置文件夹", Toast.LENGTH_LONG).show();
                    return;
                }
                Integer position = new Random().nextInt(DataHolder.files.size());
                while (haveSee.contains(position)) {
                    position = new Random().nextInt(DataHolder.files.size());
                }
                Player.play(MainActivity.this, DataHolder.files.get(position));
                haveSee.add(position);
            }
        });

        findViewById(R.id.tiaoZhuan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new CheckRootThread(new CheckRootThread.CallBack() {
                    @Override
                    public void callFiles(SmbFile[] files) {
                        mAdapter.onKeyGo(Arrays.asList(files));
                    }
                }, "smb://192.168.1.100/移动磁盘-C/TDDOWNLOAD/aaaaa/aaaaa/aaaaaa/H/test/").start();
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (mAdapter.isRoot()) {
            super.onBackPressed();
            finish();
            System.exit(0);
        } else {
            mAdapter.goToPre();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(this, PlayFileService.class);
        stopService(intent);
    }
}
